abstract class Janela{
  
  protected PFont font;
  
  protected ArrayList squareObjects = new ArrayList();
  
  protected boolean active = false;
  protected boolean paused = false;
  
  protected boolean state, pstate;
  
  abstract public void update();
  public void init(){};
  public void objectClick(){};  

  public Janela(){
    Janelas.add(this);
  }
  
  public boolean active(){return active;}
  
  public void pause(Janela j){
    JanelaPausada.add(this);
    j.init();
    active = false;
    j.active = true;
  }  
  
  public void resume(){
    JanelaAnterior = this;
    ((Janela) JanelaPausada.get(JanelaPausada.size())).init();
    ((Janela) JanelaPausada.get(JanelaPausada.size())).active = true;
    active = false;
    JanelaPausada.remove(JanelaPausada.size());
  }
  
  public void setActive(Janela j){
    if (j == null) return;
    JanelaAnterior = this;
    j.init();
    active = false;
    j.active = true;
  }
  public void setActive(){
    init();
    active = true;
  }
  
  public void objectsDisplay(){
    for (Object aux : squareObjects) ((squareObject) aux).display();
  }

  public void addButton(String id, float x, float y, float w, float h, String s){
    squareObjects.add(new rectButton(id, x, y, w, h, s));
  }

  public void addEditBox(String id, float x, float y){
    squareObjects.add(new editBox(id, x, y));
  }

  public void addEditBox(String id, float x, float y, float w, float h){
    squareObjects.add(new editBox(id, x, y, w, h));
  }
  
  public void addLabel(String id, float x, float y, String s){
    squareObjects.add(new Label(id, x, y, s));
  }
  
  public void addLabel(String id, float x, float y, String s, PFont f){
    squareObjects.add(new Label(id, x, y, s, f));    
  }
  
  public void setCaption(String id, String nCaption){
    for (Object aux : squareObjects){
      if (((squareObject) aux).getId().equals(id)){
        ((Label) aux).setCaption(nCaption);
        break;
      }
    }
  }
  
  public void removeObject(String id){
    for (Object aux : squareObjects){
      if (((squareObject) aux).getId().equals(id)){
        squareObjects.remove(aux);
        break;
      }
    }
  }

  public void clearObjects(){
      squareObjects.clear();
  }
  
  public String getObjectClickedId(){
    state = mousePressed;
    if (state && !pstate){
      for (Object aux : squareObjects)
        if (((squareObject) aux).clicked()){
          pstate = state;
          if (aux.getClass().getSimpleName().equals("editBox")) setFocus(((squareObject) aux).getId());
          return ((squareObject) aux).getId();
        }
    }
    if (!state) pstate = false;
    //Deve retornar "" pois null faria com que a string que recebe este valor tivesse que checar por null desnecessariamente
    //uma vez que nenhum botão recebe o id "" ele já serve como valor valido, e implica que nenhum botão foi clicado.
    return "";
  }

  public void setFocus(String id){
    for (Object aux : squareObjects)
      if (aux.getClass().getSimpleName().equals("editBox"))
        ((editBox) aux).setFocus(((squareObject) aux).getId().equals(id));
  }
  
  public String getEditBoxText(String id){
    for (Object aux : squareObjects)
      if (aux.getClass().getSimpleName().equals("editBox") && ((editBox) aux).getId().equals(id))
        return ((editBox) aux).getText();
    return null;
  }
  
}
